package xmlParser;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class SaxContext {
	
	private HashMap<String,List<SAXBean>> fMap = null;
	
	public SaxContext(HashMap<String,List<SAXBean>> map){
		fMap = map;
	}
	
	public int parseInt(String str){
		int result = 0;
		try{
			result = Integer.parseInt(str);
		}catch(Exception e){
			//e.printStackTrace();
		}
		return result; 
	}
	
	public float parseFloat(String str){
		float result = 0f;
		try{
			result = Float.parseFloat(str);
		}catch(Exception e){
			//e.printStackTrace();
		}
		return result; 
	}
	
	public String urlEncode(String url){
		String result = "";
		try{
			result = URLEncoder.encode(url, "gbk");
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	public String urlEncodeUtf8(String url){
		String result = "";
		try{
			result = URLEncoder.encode(url, "UTF-8");
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	public String split(String result,String regular,int position){
		try{
			if("|".equals(regular)){
				regular = "\\|";
			}
			String d[] = result.split(regular);
			if(d.length-1>position){
				return d[position];
			}else{
				return "";
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	public List<SAXBean> getNodes(String path){
		return this.xPathList(path);
	}
	public List<SAXBean> xPathList(String path){
		List<SAXBean> list = (List)fMap.get(path);
		if(list==null){
			return new ArrayList(0);
		}
		return list;
	}
	
	public List<SAXBean> getNodes(SAXBean bean,String path){
		return this.xPathList(bean, path);
	}
	public List<SAXBean> xPathList(SAXBean bean,String path){
		if(path!=null && !path.startsWith("/")){
			path = bean.path+"/"+path;
		}
		List<SAXBean> list = (List)fMap.get(path);
		if(list==null){
			return new ArrayList(0);
		}
		return getList(bean,list);
	}
	
	private List<SAXBean> getList(SAXBean bean,List<SAXBean> list){
		if(bean==null){
			return new ArrayList(0);
		}
		int position = bean.fPosition.length-1;
		int index = halfSearch(list,position,bean.fPosition[position]);
		
		if(index==-1){
			return new ArrayList(0);
		}
		
		int value = bean.fPosition[position];
		int left = index;
		int size = list.size();
		
		while(left>-1 && list.get(left).fPosition[position]==value){
			left--;
		}
		left++;
		
		int right = index;
		while(right<size && list.get(right).fPosition[position]==value){
			right++;
		}
		return list.subList(left, right);
	}
	
	private int halfSearch(List<SAXBean> igs,int position,int value){
		int index = -1;
		if(igs.size()!=0){
			int startPos = 0;
			int endPos = igs.size()-1;
			int middlePos = endPos/2;
			
			while(startPos<=endPos){
				int result = value - igs.get(middlePos).fPosition[position];
				if(result == 0){
					index = middlePos;
					break;
				}else if(result>0){
					startPos = middlePos+1;
				}else{
					endPos = middlePos -1;
				}
				middlePos = (startPos+endPos)/2;
			}
		}	
		return index;
	}
	
	public SAXBean getNode(SAXBean bean,String path){
		List<SAXBean> list = xPathList(bean,path);
		if(list.size()>0){
			return list.get(0);
		}
		return new SAXBean();
	}
	
	public SAXBean getNode(String path){
		List<SAXBean> list = (List)fMap.get(path);
		if(list==null){
			return new SAXBean();
		}
		return list.get(0);
	}
	
	
	
	public static void main(String[] args){
		System.out.print(new SaxContext(null).split("123|123", "\\|", 0));
	}
}
